<template>
  <div class="login_content">
    <div class="login_box">
      <!-- 头像区域 -->
      <div class="avatar_box">
        <img src="../assets/logo.png" alt />
      </div>
      <!-- 表单区域 -->
      <el-form
        ref="loginFormRef"
        :model="loginForm"
        :rules="loginFromRule"
        label-width="0px"
        class="form_box"
      >
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" prefix-icon="iconfont icon-user"></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            prefix-icon="iconfont icon-3702mima"
            type="password"
          ></el-input>
        </el-form-item>
        <!-- 按钮 -->
        <el-form-item class="form_button">
          <el-button type="primary" @click="loginFrom">登录</el-button>
          <el-button type="info" @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 实现表单数据绑定的对象
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      loginFromRule: {
        username: [
          { required: true, message: '请输入登录名称', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入登录密码', trigger: 'blur' },
          { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
        ]
      }
    };
  },
  methods: {
    resetForm() {
      // 对表单进行重置
      this.$refs.loginFormRef.resetFields();
    },
    loginFrom() {
      this.$refs.loginFormRef.validate(async valid => {
        // 发送axios请求
        // 返回的数据是经过拦截器之后的数据 是真实的数据，并且是请求成功之后返回的
        const data = await this.$http.post('login', this.loginForm);
        // if (res.meta.status !== 200) return this.$message.error('登录失败！');
        // console.log(data);

        // 如果请求回来的数据不是null 也就是状态值不为200的直接跳出，否则登录成功
        if (!data) return;
        this.$message.success('登录成功！');
        // 成功之后要将token 保存跳转到home 页面
        window.sessionStorage.setItem('token', data.token);
        // 利用编程式导航到下个页面
        this.$router.push('/home');
      });
    }
  }
};
</script>

<style lang="less" scope>
.login_content {
  height: 100%;
  background-color: #2b4b6b;
  .login_box {
    width: 450px;
    height: 300px;
    background-color: #fff;
    border-radius: 3px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    .avatar_box {
      width: 120px;
      height: 120px;
      border: 1px solid #eee;
      border-radius: 50%;
      padding: 12px;
      box-shadow: 0 0 10px #ddd;
      position: absolute;
      left: 50%;
      transform: translate(-50%, -50%);
      background-color: #ffffff;
      img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
        border: 1px solid #eee;
        background-color: #eee;
      }
    }
  }
}
.form_box {
  padding: 20px;
  position: absolute;
  bottom: 0;
  width: 100%;
  box-sizing: border-box;
  .form_button {
    display: flex;
    justify-content: flex-end;
  }
}
</style>
